/*
*Copyright (c) Nikhil Jain.
*Project ICARUS, communications and 
*artificial intelligence.
*/



/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
 */
package javadrone.client.access;

//~--- non-JDK imports --------------------------------------------------------

import com.lti.civil.*;
import com.lti.civil.awt.AWTImageConverter;

import javadrone.RemoteImage;
import javadrone.Server;

import javadrone.client.DroneClient;

//~--- JDK imports ------------------------------------------------------------

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import java.rmi.RemoteException;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.imageio.ImageIO;

/**
 *
 * @author Nikzzay
 */
public class CamImageProvider implements CaptureObserver, ImageProvider {
    private CaptureStream captureStream = null;
    private ImageQueue    ib;

    public CamImageProvider(ImageQueue ib) throws CaptureException {
        CaptureSystemFactory factory = DefaultCaptureSystemFactorySingleton.instance();
        CaptureSystem        system;

        system = factory.createCaptureSystem();
        system.init();

        List list = system.getCaptureDeviceInfoList();
        int  i    = 0;

        System.out.println("No. of cameras detected: " + list.size());

        if (i < list.size()) {
            CaptureDeviceInfo info = (CaptureDeviceInfo) list.get(i);

            System.out.println(
                (new StringBuilder()).append("Device ID ").append(i).append(": ").append(
                    info.getDeviceID()).toString());
            System.out.println(
                (new StringBuilder()).append("Description ").append(i).append(": ").append(
                    info.getDescription()).toString());
            captureStream = system.openCaptureDeviceStream(info.getDeviceID());
            captureStream.setObserver(CamImageProvider.this);
        }

        this.ib = ib;
    }

    @Override
    public void start() throws CaptureException {
        captureStream.start();
    }

    @Override
    public void stop() throws CaptureException {
        captureStream.stop();
    }

    @Override
    public void onNewImage(CaptureStream stream, Image image) {
        byte[] bytes = null;

        if (image == null) {
            return;
        }

        try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
            ImageIO.write(AWTImageConverter.toBufferedImage(image), "jpeg", os);
            bytes = os.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
        }

        ib.add(new RemoteImage(bytes, System.currentTimeMillis() + DroneClient.TIMEDIFF()));

        // System.out.println("Shot taken..."+iQueue.size());
    }

    @Override
    public void onError(CaptureStream stream, CaptureException ce) {
        System.out.println("Error!");
    }

    @Override
    public void attach(ImageQueue iQ) {
        this.ib = iQ;
    }
}


//~ Formatted by Jindent --- http://www.jindent.com
